import time
from datetime import datetime
import uuid

from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import Any

from config.database import Base
from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime


def gen_uuid():
    timestamp = str(int(time.time()))

    return uuid.uuid5(uuid.NAMESPACE_DNS, str(timestamp) + str(uuid.uuid4()))


class ReviewsModel(Base):
    __tablename__ = 'tb_reviews'
    __table_args__ = {
        'schema': 'reviews'
    }

    reviews_id = Column(String(32), default=gen_uuid(), primary_key=True, comment='表索引主键')
    reviews_title = Column(String(50), nullable=False, comment='点评标题')
    reviews_content = Column(String(200), default='', comment='点评内容')
    primary_image = Column(String(255), comment='点评示例图')
    reviews_images = Column(postgresql.ARRAY(String), comment='点评全部图片数组')
    user_id = Column(String(32), comment='所属用户ID')
    reviews_author = Column(String(30), nullable=False, comment='点评作者')
    reviews_avatar = Column(String(255), nullable=False, comment='点评作者头像')
    reviews_group_code = Column(Integer, nullable=False, comment='点评分组编码')
    group_chain = Column(String(100), comment='分类串')
    is_hot = Column(Boolean, default=False, comment='是否热门')
    mark = Column(Float(1, 1), default=3.5, comment='评分')
    is_release = Column(Boolean, default=False, comment='是否已发布')
    release_time = Column(DateTime(timezone=True), comment='发布时间')
    create_time = Column(DateTime(timezone=True), default=datetime.now(), comment='创建保存时间')
    last_modify_time = Column(DateTime(timezone=True), default=datetime.now(), comment='最后编辑时间')
    liker_num = Column(Integer, default=0, comment='被喜爱数')
    del_flag = Column(Boolean, default=False, comment='删除标识')

    def __init__(self, **kw: Any):
        super().__init__(**kw)
        self.user_id = gen_uuid()
